package com.slh.touchandroid.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.MenuItem;
import android.view.View;

import com.slh.touchandroid.R;
import com.slh.touchandroid.receiver.Broadcast;
import com.slh.touchandroid.utils.CONFIG;

public class SettingsActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	public static void start(Context context) {
		Intent intent = new Intent(context, SettingsActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		setupSimplePreferencesScreen();
		initActionBar();
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	private void initActionBar() {
		if (Build.VERSION.SDK_INT >= 11) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
			getActionBar().setBackgroundDrawable(
					new ColorDrawable(Color.parseColor("#0548b7")));
		} else {
			View title = getWindow().findViewById(android.R.id.title);
			if (title != null) {
				View titleBar = (View) title.getParent();
				titleBar.setBackgroundDrawable(new ColorDrawable(Color
						.parseColor("#0548b7")));	
			}
			
		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			onBackPressed();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@SuppressWarnings("deprecation")
	private void setupSimplePreferencesScreen() {
		addPreferencesFromResource(R.xml.pref_general);
		PreferenceManager.getDefaultSharedPreferences(this)
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		boolean value = sharedPreferences.getBoolean(key, true);
		if (getString(R.string.key_display_androidtouch).equals(key)) {
			Broadcast.sendBroadcast(getApplicationContext(),
					CONFIG.BROADCAST.ACTION_FLOATING_ICON_SETTINFS,
					CONFIG.BROADCAST.EXTRA_FLOATING_ICON_SETTINFS, value);
		} else if (getString(R.string.key_display_androidtouch_on_lock_screen)
				.equals(key)) {
			Broadcast.sendBroadcast(getApplicationContext(),
					CONFIG.BROADCAST.ACTION_SHOW_ICON_ON_LOCK_SETTINFS,
					CONFIG.BROADCAST.EXTRA_SHOW_ICON_ON_LOCK_SETTINFS, value);
		} else if (getString(R.string.key_lock).equals(key)) {
			CONFIG.ADMIN_DEVICE.toggleDeviceAdmin(getApplicationContext(),
					value);
		}

	}

}
